package 线程.综合练习.抽奖箱抽奖;

import java.util.ArrayList;
import java.util.Collections;

public class MTh extends Thread{
    //定义一个
     ArrayList<Integer> list;

     //创建2个集合用于存储抽奖的数据
    public  MTh(ArrayList<Integer> list){
        this.list=list;
    }
    @Override
    public void run() {
        ArrayList<Integer> box=new ArrayList<>();
        while (true) {
            synchronized (MTh.class){
                //判断奖池是否抽完
                if (list.size()==0){
                    System.out.println(getName()+box);
                    int sum=0;
                    for (Integer integer : box) {
                        sum+=integer;
                    }
                    Integer max = Collections.max(box);
                    System.out.println(getName()+"的最高金额为"+max+",总金额高达"+sum+"元");
                    break;
                }else {
                    //从奖池进行随机抽奖
                    //打乱奖池
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    box.add(prize);
                }
            }
            try {
                sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
